package com.example.controller;

import com.alibaba.excel.EasyExcel;
import com.example.pojo.Aircraft;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class ExcelExportController {
    @GetMapping("/exportExcelTemplate")
    public void exportExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String fileName = URLEncoder.encode("机票导入模板.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);

        // 创建一个空的数据集，这里只是模板，没有实际数据
        List<Aircraft> dataList = new ArrayList<>();

        // 写入Excel
        EasyExcel.write(response.getOutputStream(), Aircraft.class)
                .sheet("Sheet1")
                .doWrite(dataList);
    }
}
